
/****************************************************************************************/
/*     	File Name:	Table S4.do			        				                */
/*     	Date:   	June 2025    			            				                */
/*      Author: 	Juan Acevedo-Ossa		                        	                */
/*      Purpose:   Replication file for "The Indirect Effects of	                    */
/*				   Structural Power										                */
/*      Input Files:  tradenousnoch.csv, tradeonlyus.csv tradeonlych.csv                */	
/*                    militarynousnoch.csv, militaryonlyus.csv militaryonlych.csv       */	
/*                    patentsnousnoch.csv, patentsonlyus.csv patentsonlych.csv          */                              	
/*                    investmentnousnoch.csv, investmentonlyus.csv investmentonlych.csv	*/	
/*      Output File:   "aspects.rtf"                                                    */
/*      Total Processing time:  1 hrs 40 minutes                                        */
/*      Machine:  16.0 GB RAM - Intel Core i7 @ 2.50 GHz 16 cores                       */
/*      Software: Stata 18 SE                                                           */
/****************************************************************************************/

* This is the base code for the mSTAR models including Winecoff's (2020) operationalizations


**********************************************************************************************************
*ESTIMATION ML (computationally expensive. Stata doesn't allow multiple Dvarlags with spregress and ML)
*********************************************************************************************************

*1. Load program made by Rob Franzese et. al.


log using tableS4.txt, text

 
timer clear
timer on 1


drop _all

pr drop _all

set more off
*pause off
*set rmsg on
*set rmsg off

***********************
*LIKELIHOOD  EVALUATOR*
***********************
                                                        
program define splag_ll

*Instruct program to use "lnf," "mu," "rho1," "rho2," "rho3," and "sigma" are arg's to program
args lnf mu rho1 rho2 rho3 sigma

*Instruct "A," "rSL1," "rSL2," "rSL3," and "rSL4" to be temporary variables
*Code for class example drops 4th ((time-lagged) endogenous) spatial-lag
tempvar A rSL1 rSL2 rSL3 /* rSL4 */

*Define 'rSL1'
gen double `rSL1'=`rho1'*SL1

*Define 'rSL2'
gen double `rSL2'=`rho2'*SL2

*Define 'rSL3'
gen double `rSL3'=`rho3'*SL3

*Define 'rSL4'
*gen double `rSL4'=`rho4'*SL4

*Define p1 (coefficient on spatial lag 1)
scalar p1 = `rho1'

*Define p2 (coefficient on spatial lag 2)
scalar p2 = `rho2'

*Define p3 (coefficient on spatial lag 3)
scalar p3 = `rho3'

*Define p4 (coefficient on spatial lag 4)
*scalar p4 = `rho4'

*Define 1st spatial-lag matrix
matrix p1W1 = p1*W1

*Define 2nd spatial-lag matrix
matrix p2W2 = p2*W2

*Define 3rd spatial-lag matrix
matrix p3W3 = p3*W3



*Define master (IpW) combined spatial lag matrix
matrix IpW = I_n - p1W1 - p2W2- p3W3

*Define 'A' and make into scalar
qui gen double `A' = ln(det(IpW))/$nobs if _n == 1
scalar A = `A'

*Define 'lnf' and end program
*qui replace `lnf'= A + ln(normden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
* For Stata9 & before: use "normden" instead of "normalden"

* LOG LIKELHOOD:
*qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`rSL4'-`mu', 0, `sigma'))
qui replace `lnf'= A + ln(normalden($ML_y1-`rSL1'-`rSL2'-`rSL3'-`mu', 0, `sigma'))

end


**************************************
*LOAD MATRICES IN THE TRADITIONAL WAY*
**************************************


cd "`c(current_do)'"

*Indicate the dependent variable you want to test:


global Y alignuschdiff


*Indicate set of X values for each corresponding phi (time-lagged DV)

global X lalignuschdiff laidusgdplog sanct_stock ltradegdpuslog polyarchy pop_log loggdppercapita corruption_control imf atop right_wing dip_visits_usa unsc_non_p developing


local i 1

forvalues i=1/4 {

drop _all
matrix drop _all

if `i'== 1 {
	local structure "trade"
}
else if `i' == 2 {
	local structure  "military"
}
else if `i' == 3 {
	local structure "patents"
}
else if `i' == 4 {
	local structure "investment"
}


*All countries but China and the US
import delimited "Data/Aspects/`structure'nousnoch.csv", rowrange(2) varnames(nonames) clear
qui sum v1
global nobs = r(N)
mkmat v1-v$nobs, matrix(W1)
matrix I_n = I($nobs)

*Only the US
drop _all
import delimited "Data/Aspects/`structure'onlyus.csv", rowrange(2) varnames(nonames) clear
mkmat v1-v$nobs, matrix(W2)

*Only China
drop _all
import delimited "Data/Aspects/`structure'onlych.csv", rowrange(2) varnames(nonames) clear
mkmat v1-v$nobs, matrix(W3)


drop _all

import delimited "Data/panel_unga.csv", clear 



if `i' == 4{
	drop if year==2000
}


*Designate Y variable







*Create Y-variable as a matrix (vector)
mkmat $Y, matrix(Y)

*Create 1st spatial-lag in matrix and then save as variable also
matrix SL1 = W1*Y
svmat SL1, n(SL1)

*Create 2nd spatial-lag in matrix and then save as variable also
matrix SL2 = W2*Y
svmat SL2, n(SL2)

*Create 3rd spatial-lag in matrix and then save as variable also
matrix SL3 = W3*Y
svmat SL3, n(SL3)


**************************************
*MODEL 1*
**************************************


*PRODUCE STARTING VALUES VIA OLS


qui regress $Y $X
global nobs=e(N)
matrix OLSb=e(b)
global OLSsigma=e(rmse)




*ESTIMATE SPATIAL LAG MODEL*


*Designate model to estimate
ml model lf splag_ll (mu: $Y=$X) (rho1:) (rho2:) (rho3:) (sigma:) /*(rho4:)*/

*Specify starting values
ml init OLSb
ml init rho1:_cons=0
ml init rho2:_cons=0
ml init rho3:_cons=0
*ml init rho4:_cons=0
ml init sigma:_cons=$OLSsigma
* ml max, difficult trace
ml max, trace

estimates store `structure'
estat ic


}


*Print for tableS4
esttab trade investment patents military, ///
cells("b(star fmt(4))" se(par fmt(4))) ///
modelwidth(10) nodepvars ///
varlabels(lalignuschdiff "Vote coincidence(t-1)" ///
          laidusgdplog "log(USAID/GDP)" ///
          sanct_stock "Sanctions" ///
          ltradegdpuslog "log(TradeUS/GDP) (t-1)" ///
          polyarchy "Democracy" ///
		  pop_log "log(Population)" ///
		  loggdppercapita "log(GDP per capita)" ///
          corruption_control "Control of corruption" ///
          imf "IMF commitment" ///
          atop "Alliances" ///
          right_wing "Right Wing" ///
          dip_visits_usa "Diplomatic" ///
          unsc_non_p "UNSC non-p" ///
		  developing "Developing") ///
stats(N aic bic ll, fmt(%9.3f) label("Observations" "AIC" "BIC" "LL")) ///
title("mSTAR models of vote coincidence") /// 
addnotes("* p-value < 0.10; ** p-value < 0.05 level; *** p-value < 0.01 level.")


timer off 1
timer list
log close